package com.wtf.dsp.admin.enums;

import com.baomidou.mybatisplus.annotation.DbType;
import lombok.ToString;

import java.util.Arrays;

/**
 * 数据库表SQL仓库（TODO 待迁移到数据库表中）
 * date: 2022/6/18 10:59
 * @author linhuanyou
 * @email 496558736@qq.com
 * @version 1.0
 */
@ToString
public enum DbTableSqlRepository {
    MYSQL(DbType.MYSQL,"select table_name tableName, engine, table_comment tableComment, create_time createTime from information_schema.tables where table_schema = (select database()) <#{tableName}> and table_name like '%#{tableName}%' </#{tableName}> order by create_time desc","select table_name tableName, engine, table_comment tableComment, create_time createTime from information_schema.tables where table_schema = (select database()) and table_name = '#{tableName}'","select column_name columnName, data_type dataType, column_comment columnComment, column_key columnKey, extra from information_schema.columns where table_name = '#{tableName}' and table_schema = (select database()) order by ordinal_position"),
    POSTGRE_SQL(DbType.POSTGRE_SQL,"select t1.schemaname as schemaName,t1.tablename as tableName, obj_description(relfilenode, 'pg_class') as tableComment, now() as createTime from pg_tables t1, pg_class t2 where t1.tablename not like 'pg%' and t1.tablename not like 'sql_%' and t1.tablename = t2.relname <#{tableName}> and t1.tablename like '%#{tableName}%' </#{tableName}>","select t1.schemaname as schemaName,t1.tablename as tableName, obj_description(relfilenode, 'pg_class') as tableComment, now() as createTime from pg_tables t1, pg_class t2 where t1.tablename = '#{tableName}' and t1.tablename = t2.relname","select t2.attname as columnName, pg_type.typname as dataType, col_description(t2.attrelid,t2.attnum) as columnComment, '' as extra,\n" +
            "\t(CASE t3.contype WHEN 'p' THEN 'PRI' ELSE '' END) as columnKey\n" +
            "from pg_class as t1, pg_attribute as t2 inner join pg_type on pg_type.oid = t2.atttypid\n" +
            "\tleft join pg_constraint t3 on t2.attnum = t3.conkey[1] and t2.attrelid = t3.conrelid\n" +
            "where t1.relname = '#{tableName}' and t2.attrelid = t1.oid and t2.attnum>0"),
    ;

    private DbType dbType;

    private String tableList;

    private String table;

    private String columns;

    DbTableSqlRepository(DbType dbType, String tableList, String table, String columns) {
        this.dbType = dbType;
        this.tableList = tableList;
        this.table = table;
        this.columns = columns;
    }

    public static DbTableSqlRepository valueOf(DbType dbType) {
       return Arrays.stream(values()).filter(e->{return e.getDbType().equals(dbType);}).findFirst().get();
    }

    public DbType getDbType() {
        return dbType;
    }

    public void setDbType(DbType dbType) {
        this.dbType = dbType;
    }

    public String getTableList() {
        return tableList;
    }

    public void setTableList(String tableList) {
        this.tableList = tableList;
    }

    public String getTable() {
        return table;
    }

    public void setTable(String table) {
        this.table = table;
    }

    public String getColumns() {
        return columns;
    }

    public void setColumns(String columns) {
        this.columns = columns;
    }

}
